package itheima.com.tabhostdemo;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends AppCompatActivity {

    private FragmentTabHost tabhost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //1.引用FragmentTabhost
        tabhost = (FragmentTabHost) findViewById(R.id.tabhost);

        /**2. 初始化FragmentTabhost
         * params: context:上下文
         * FragmentManager:片段管理器
         * containerId:容纳片段的容器id
         */
        tabhost.setup(this,getSupportFragmentManager(),R.id.fl_content);

        //3.向FragmentTabhost添加Tab
/*        //3.1 准备TabSpec
        TabHost.TabSpec tabSpec1 = tabhost.newTabSpec("news");
        tabSpec1.setIndicator("综合");//设置页签的指示器，可以是字符串或者View
       // abSpec1.setIndicator(View);
        //3.2 准备每个tab关联的Fragment

        *//**3.3 向FragmentTabhost添加tab
         * TabSpec:TabSpec对象
         * Class： 当前Tab关联的Fragment
         * bundle： 向Fragment传递的参数
         *//*
        tabhost.addTab(tabSpec1,DefaultFragment.class,getBundle("综合"));

        //添加动弹标签
        TabHost.TabSpec tabSpec2 = tabhost.newTabSpec("tweets");
        tabSpec2.setIndicator("动弹");//设置页签的指示器，可以是字符串或者View
        tabhost.addTab(tabSpec2,DefaultFragment.class,getBundle("动弹"));*/

        //3.通过枚举方式向FragmentTabhost添加Tab
        addTab();
    }


    //添加tab
    private void addTab() {
        MainTab[] values = MainTab.values();
        for (int i=0;i<values.length;i++){
            MainTab maintab=values[i];
            TabHost.TabSpec tabspec=tabhost.newTabSpec(maintab.getTag());
            tabspec.setIndicator(maintab.getTitle());
            tabhost.addTab(tabspec,maintab.getClz(),getBundle(maintab.getTitle()));
        }
    }

    private Bundle getBundle(String args) {
        Bundle bundle=new Bundle();
        bundle.putString("key",args);
        return  bundle;
    }
}
